#!/usr/bin/env python
# -*- coding: utf-8 -*-

from datetime import datetime, timedelta

from typing_extensions import overload

from web.config.commodity_future_hour_data_config import CommodityFutureHourDataConfig
from web.util.dto_util import DtoUtil
from web.manager.log_manager import LogManager
from web.service.base_service import BaseService

Logger = LogManager.get_logger(__name__)

"""
CommodityFutureHData的service类
"""


class CommodityFutureHourDataService(BaseService):

    def add_oracle_column_comment(self):
        """
        为表commodity_future_hour_data添加注释
        """

        Logger.info('为表commodity_future_hour_data添加注释')

        self.commodity_future_hour_data_dao.add_oracle_column_comment()

    # @overload
    def save_commodity_future_hour_data_dto_list(self, commodity_future_hour_data_list: list):
        """
        保存CommodityFutureHourData类型对象列表
        """

        Logger.info('保存CommodityFutureHourData类型对象列表')

        for commodity_future_hour_data_dto in commodity_future_hour_data_list:
            commodity_future_hour_data = DtoUtil.commodity_future_data_dto_to_commodity_future_hour_data(
                commodity_future_hour_data_dto)
            # try:
            if isinstance(commodity_future_hour_data.close_price, str) or isinstance(
                    commodity_future_hour_data.highest_price, str) or isinstance(
                commodity_future_hour_data.open_price, str) or isinstance(
                commodity_future_hour_data.lowest_price, str) or isinstance(
                commodity_future_hour_data.price_change, str) or isinstance(
                commodity_future_hour_data.rising_and_falling_amount, str) or isinstance(
                commodity_future_hour_data.volume, str):
                Logger.warn('期货[%s]在交易日[%s]没有数据', commodity_future_hour_data.code,
                            datetime.now().strftime('%Y-%m-%d %H:%M:%S'))
                continue
            else:
                _datetime = datetime.now()
                # 计算begin_time和end_time
                commodity_future_hour_data.begin_time = _datetime
                commodity_future_hour_data.end_time = _datetime
                minute = int(_datetime.strftime('%M'))
                second = int(_datetime.strftime('%S'))
                millisecond = int(_datetime.strftime('%f')[:-3])

                commodity_future_hour_data.begin_time = commodity_future_hour_data.begin_time.replace(
                    minute=0, second=0, microsecond=0)
                commodity_future_hour_data.begin_time = commodity_future_hour_data.begin_time - timedelta(
                    hours=1)
                commodity_future_hour_data.end_time = commodity_future_hour_data.end_time.replace(
                    minute=0,
                    second=0,
                    microsecond=0)

            commodity_future_hour_data.save()
        # except DataError as e:
        #     Logger.error(e)
        # except ValidationError as e:
        #     Logger.error(e, commodity_future_hour_data)
        # except:
        #     Logger.error('未知错误，需要处理')

    # @overload
    def save_commodity_future_hour_data_list(self, commodity_future_hour_data_list: list):
        """
        保存CommodityFutureHourData类型对象列表
        """

        self.commodity_future_hour_data_dao.save_batch(commodity_future_hour_data_list)

    ########################################### 全量计算数据 ########################################

    def write_hour_basic_data(self):
        """
        计算1小时级别全部last_close_price、rising_and_falling_amount和price_change字段
        """

        Logger.info("计算1小时级别全部last_close_price、rising_and_falling_amount和price_change字段")

        self.commodity_future_hour_data_dao.write_hour_basic_data()

    def write_hour_all_ma(self):
        """
        计算1小时级别全部MA数据
        """

        Logger.info("计算1小时级别全部MA数据")

        self.commodity_future_hour_data_dao.write_hour_all_ma()

    def write_hour_all_macd(self):
        """
        计算1小时级别全部MACD数据
        """

        Logger.info("计算1小时级别全部MACD数据")

        self.commodity_future_hour_data_dao.write_hour_all_macd()

    def write_hour_all_kd(self):
        """
        计算1小时级别全部KD数据
        """

        Logger.info("计算1小时级别全部KD数据")

        self.commodity_future_hour_data_dao.write_hour_all_kd()

    def write_hour_all_boll(self):
        """
        计算1小时级别全部boll数据
        """

        Logger.info("计算1小时级别全部boll数据")

        self.commodity_future_hour_data_dao.write_hour_all_boll()

    def write_hour_all_hei_kin_ashi(self):
        """
        计算1小时级别全部hei_kin_ashi数据
        """

        Logger.info("计算1小时级别全部hei_kin_ashi数据")

        self.commodity_future_hour_data_dao.write_hour_all_hei_kin_ashi()

    def write_hour_all_bias(self):
        """
        计算1小时级别全部bias数据
        """

        Logger.info("计算1小时级别全部bias数据")

        self.commodity_future_hour_data_dao.write_hour_all_bias()

    def write_hour_all_variance(self):
        """
        计算1小时级别全部variance数据
        """

        Logger.info("计算1小时级别全部variance数据")

        self.commodity_future_hour_data_dao.write_hour_all_variance()

    def write_hour_unite_relative_price_index(self):
        """
        计算1小时级别全部unite_relative_price_index数据
        """

        Logger.info("计算1小时级别全部unite_relative_price_index数据")

        self.commodity_future_hour_data_dao.write_hour_unite_relative_price_index()

    ########################################### 增量计算数据 ########################################

    def write_hour_basic_data_by_date(self):
        """
        根据日期，计算1小时级别某一个交易日的last_close_price、rising_and_falling_amount和price_change字段
        """

        Logger.info(
            "根据日期，计算1小时级别交易时间从[%s]至[%s]的last_close_price、rising_and_falling_amount和price_change字段",
            CommodityFutureHourDataConfig.Commodity_Future_Hour_Data_Basic_Data_By_Begin_Time,
            CommodityFutureHourDataConfig.Commodity_Future_Hour_Data_Basic_Data_By_End_Time)

        self.commodity_future_hour_data_dao.write_hour_basic_data_by_date(
            CommodityFutureHourDataConfig.Commodity_Future_Hour_Data_Basic_Data_By_Begin_Time,
            CommodityFutureHourDataConfig.Commodity_Future_Hour_Data_Basic_Data_By_End_Time)

    def write_date_unite_relative_price_index_by_date(self):
        """
        根据日期，计算1小时级别某一个交易日的unite_relative_price_index数据
        """

        Logger.info("根据日期，计算1小时级别交易时间从[%s]至[%s]的unite_relative_price_index数据",
                    CommodityFutureHourDataConfig.Commodity_Future_Hour_Data_Unite_Relative_Price_Index_By_Begin_Time,
                    CommodityFutureHourDataConfig.Commodity_Future_Hour_Data_Unite_Relative_Price_Index_By_End_Time)

        self.commodity_future_hour_data_dao.write_hour_unite_relative_price_index_by_date(
            CommodityFutureHourDataConfig.Commodity_Future_Hour_Data_Unite_Relative_Price_Index_By_Begin_Time,
            CommodityFutureHourDataConfig.Commodity_Future_Hour_Data_Unite_Relative_Price_Index_By_End_Time)

    def write_hour_ma_by_date(self):
        """
        根据日期，计算1小时级别某一个交易日的MA数据
        """

        Logger.info("根据日期，计算1小时级别交易时间从[%s]至[%s]的MA数据",
                    CommodityFutureHourDataConfig.Commodity_Future_Hour_Data_Ma_By_Begin_Time,
                    CommodityFutureHourDataConfig.Commodity_Future_Hour_Data_Ma_By_End_Time)

        self.commodity_future_hour_data_dao.write_hour_ma_by_date(
            CommodityFutureHourDataConfig.Commodity_Future_Hour_Data_Ma_By_Begin_Time,
            CommodityFutureHourDataConfig.Commodity_Future_Hour_Data_Ma_By_End_Time)

    def write_hour_macd_by_date(self):
        """
        根据日期，计算1小时级别某一个交易日的MACD数据
        """

        Logger.info("根据日期，计算1小时级别交易时间从[%s]至[%s]的MACD数据",
                    CommodityFutureHourDataConfig.Commodity_Future_Hour_Data_Macd_By_Begin_Time,
                    CommodityFutureHourDataConfig.Commodity_Future_Hour_Data_Macd_By_End_Time)

        self.commodity_future_hour_data_dao.write_hour_macd_by_date(
            CommodityFutureHourDataConfig.Commodity_Future_Hour_Data_Macd_By_Begin_Time,
            CommodityFutureHourDataConfig.Commodity_Future_Hour_Data_Macd_By_End_Time)

    def write_hour_kd_by_date(self):
        """
        根据日期，计算1小时级别某一个交易日的KD数据
        """

        Logger.info("根据日期，计算1小时级别交易时间从[%s]至[%s]的KD数据",
                    CommodityFutureHourDataConfig.Commodity_Future_Hour_Data_Kd_By_Begin_Time,
                    CommodityFutureHourDataConfig.Commodity_Future_Hour_Data_Kd_By_End_Time)

        self.commodity_future_hour_data_dao.write_hour_kd_by_date(
            CommodityFutureHourDataConfig.Commodity_Future_Hour_Data_Kd_By_Begin_Time,
            CommodityFutureHourDataConfig.Commodity_Future_Hour_Data_Kd_By_End_Time)

    def write_hour_boll_by_date(self):
        """
        根据日期，计算1小时级别某一个交易日的boll数据
        """

        Logger.info("根据日期，计算1小时级别交易时间从[%s]至[%s]的boll数据",
                    CommodityFutureHourDataConfig.Commodity_Future_Hour_Data_Boll_By_Begin_Time,
                    CommodityFutureHourDataConfig.Commodity_Future_Hour_Data_Boll_By_End_Time)

        self.commodity_future_hour_data_dao.write_hour_boll_by_date(
            CommodityFutureHourDataConfig.Commodity_Future_Hour_Data_Boll_By_Begin_Time,
            CommodityFutureHourDataConfig.Commodity_Future_Hour_Data_Boll_By_End_Time)

    def write_hour_hei_kin_ashi_by_date(self):
        """
        根据日期，计算1小时级别某一个交易日的hei kin ashi数据
        """

        Logger.info("根据日期，计算1小时级别交易时间从[%s]至[%s]的hei kin ashi数据",
                    CommodityFutureHourDataConfig.Commodity_Future_Hour_Data_Hei_Kin_Ashi_By_Begin_Time,
                    CommodityFutureHourDataConfig.Commodity_Future_Hour_Data_Hei_Kin_Ashi_By_End_Time)

        self.commodity_future_hour_data_dao.write_hour_hei_kin_ashi_by_date(
            CommodityFutureHourDataConfig.Commodity_Future_Hour_Data_Hei_Kin_Ashi_By_Begin_Time,
            CommodityFutureHourDataConfig.Commodity_Future_Hour_Data_Hei_Kin_Ashi_By_End_Time)

    def write_hour_variance_by_date(self):
        """
        根据日期，计算1小时级别某一个交易日的variance数据
        """

        Logger.info("根据日期，计算1小时级别交易时间从[%s]至[%s]的variance数据",
                    CommodityFutureHourDataConfig.Commodity_Future_Hour_Data_Variance_By_Begin_Time,
                    CommodityFutureHourDataConfig.Commodity_Future_Hour_Data_Variance_By_End_Time)

        self.commodity_future_hour_data_dao.write_hour_variance_by_date(
            CommodityFutureHourDataConfig.Commodity_Future_Hour_Data_Variance_By_Begin_Time,
            CommodityFutureHourDataConfig.Commodity_Future_Hour_Data_Variance_By_End_Time)

    def write_hour_bias_by_date(self):
        """
        根据日期，计算1小时级别某一个交易日的BOLL数据
        """

        Logger.info("根据日期，计算1小时级别交易时间从[%s]至[%s]的BIAS数据",
                    CommodityFutureHourDataConfig.Commodity_Future_Hour_Data_Bias_By_Begin_Time,
                    CommodityFutureHourDataConfig.Commodity_Future_Hour_Data_Bias_By_End_Time)

        self.commodity_future_hour_data_dao.write_hour_bias_by_date(
            CommodityFutureHourDataConfig.Commodity_Future_Hour_Data_Bias_By_Begin_Time,
            CommodityFutureHourDataConfig.Commodity_Future_Hour_Data_Bias_By_End_Time)

    ########################################### 查询 ########################################
    def distinct_code(self):
        """
        返回code，去除重复的
        """

        Logger.info("返回code，去除重复的")

        return self.commodity_future_hour_data_dao.distinct_code()
